package dyyx;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.jdbc.SqlRunner;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.invoker.Invoker;

import dyyx.data.UserDTO;

public class UtilTest {

	public static void main(String[] args) throws Exception {
		System.out.println("hello,UtilTest,"+new Date());
		
	    Connection connection = DriverManager.getConnection("jdbc:h2:mem:testdb","sa", "");
		ScriptRunner scriptRunner = new ScriptRunner(connection);
		scriptRunner.runScript(Resources.getResourceAsReader("db/data.sql"));
		    
		SQL sql = new SQL();
		sql.SELECT("id,name,birth,height,weight")
		.FROM("t_user")
		.WHERE("id=?");
		
		SqlRunner sqlRunner = new SqlRunner(connection);

		Map<String,Object> map = sqlRunner.selectOne(sql.toString(), 1);
		connection.close();

		System.out.println(sql);
		System.out.println(map);
			

		MetaClass metaClass = MetaClass.forClass(UserDTO.class, new DefaultReflectorFactory());
		System.out.println(metaClass.hasDefaultConstructor());
		System.out.println(metaClass.hasGetter("userName"));
		System.out.println(StringUtils.join(metaClass.getGetterNames(),","));
		UserDTO user = new UserDTO();
		Invoker invoker = metaClass.getGetInvoker("userName");
		Object userName = invoker.invoke(user, null);
		System.out.println(userName);

		invoker = metaClass.getSetInvoker("userName");
		invoker.invoke(user, new Object[] {"tiger"});
		System.out.println(user);

		MetaObject metaObject = SystemMetaObject.forObject(user);

		System.out.println(metaObject.hasGetter("userName"));
		System.out.println(StringUtils.join(metaObject.getGetterNames(),","));
		System.out.println(metaObject.getGetterType("userName"));

		
		
	}
}
